package com.leo.biz.service.security;

import com.leo.biz.service.impl.Alog;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRealm extends AuthorizingRealm{
    @Autowired
    private Alog alog;

    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username=(String) token.getPrincipal();
        /*如果不等于admin，说明找不到username的认证信息，返回空的认证对象*/
        if(username.equalsIgnoreCase("admin")==false)
        {
            alog.debug("当前账号不存在:"+username);
            return null;
        }
        String newPassword = new SimpleHash(
                "md5",
                "123456",
                ByteSource.Util.bytes(username+"8d78869f470951332959580424d4bf4f")).toHex();

        SimpleAuthenticationInfo authenticationInfo=new SimpleAuthenticationInfo(username,newPassword,ByteSource.Util.bytes(username+"8d78869f470951332959580424d4bf4f"),getName());
               return authenticationInfo;
    }
}